package com.tree.thread;

import lombok.Getter;
import lombok.Setter;

import java.util.concurrent.CountDownLatch;

/**
 * @ClassName com.tree.thread
 * Description: <类功能描述>. <br>
 * <p>
 * <使用说明>
 * </p>
 * @Author tree
 * @Date 2019-10-27 11:08
 * @Version 1.0
 */
public class ThreadLocalTest {
    public static void main(String[] args) {
//        Res res = new Res();
//        InThread in = new InThread(res);
//        OutThread out = new OutThread(res);
//        in.start();
//        out.start();
        CountDownLatch countDownLatch = new CountDownLatch(2);
        new Thread(new Runnable() {

            @Override
            public void run() {
                System.out.println(Thread.currentThread().getName() + ",子线程开始执行...");
                countDownLatch.countDown();
                for (int i =0; i< 10; i++) {
                    try {
                        Thread.sleep(10);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    System.out.println(Thread.currentThread().getName() + ",子线程+" + i + "结束执行...");
                }
            }
        }).start();

        new Thread(new Runnable() {

            @Override
            public void run() {
                System.out.println(Thread.currentThread().getName() + ",子线程开始执行...");
                countDownLatch.countDown();//计数器值每次减去1
                for (int i =0; i< 10; i++) {
                    try {
                        Thread.sleep(10);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    System.out.println(Thread.currentThread().getName() + ",子线程+" + i + "结束执行...");
                }
            }
        }).start();
        try {
            countDownLatch.await();// 減去为0,恢复任务继续执行
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
//        try {
//            Thread.sleep(10);
//        } catch (InterruptedException e) {
//            e.printStackTrace();
//        }
        System.out.println("两个子线程执行完毕....");
        System.out.println("主线程继续执行.....");
        for (int i = 0; i <10; i++) {
            System.out.println("main,i:"+i);
        }


    }
}
@Setter
@Getter
class  Res{
    public String name;
    public String sex;

    /**
     * 线程通讯标识
     */
    public boolean flag = false;

}
class InThread extends Thread{
    private Res res;
    public InThread(Res res){
        this.res = res;
    }
    @Override
    public void run() {
        int count = 0;
        while (true) {
            synchronized (res) {
                if (res.flag) {
                    try {
                        res.wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                if(count == 0){
                    res.name = "name1";
                    res.sex = "男";
                }else {
                    res.name = "name2";
                    res.sex = "女";
                }

                count = (count+1)%2;
                res.flag = true;
                //唤醒当前线程
                res.notify();

            }
        }
    }
}

class OutThread extends Thread{
    private Res res;
    public OutThread (Res res){
        this.res = res;
    }
    @Override
    public void run() {
        while (true){
            synchronized (res){
                if(!res.flag){
                    try {
                        res.wait();
                    }catch (Exception e){
                    }
                }

                System.out.println(res.name+"---"+res.sex);
                res.flag = false;
                res.notify();
            }
        }
    }
}